/**
 * 
 */
package com.gopay.bis.psbc.ebank.impl;

import java.util.Map;

import com.gopay.bis.psbc.ebank.operation.PsbcB2bCertHelper;
import com.gopay.bis.psbc.ebank.operation.PsbcB2bQueryHelper;
import com.gopay.common.constants.bank.PsbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.businesses.BankService;

/**
 * @PsbcB2bBankServiceImpl.java
 * @author Ryan
 * @2013-12-2 上午10:14:21 www.gopay.com.cn Inc.All rights reserved.
 */
public class PsbcB2bBankServiceImpl implements BankService {

    public Object sign(BaseMessage msg) throws GopayException {
        String singMsg = psbcB2bCertHelper.sign(msg);
        return singMsg;
    }

    public Object verify(BaseMessage msg) throws GopayException {
        boolean isVerifyed = (Boolean) psbcB2bCertHelper.verify(msg);
        return isVerifyed;
    }

    public Object query(Object obj) throws GopayException {
        PsbcB2bMessage message = (PsbcB2bMessage) obj;
        // sign
        String signStr = psbcB2bCertHelper.sign(message);
        message.setSignMessage(signStr);
        // build query parameter
        Map<String, String> queryParam = psbcB2bQueryHelper.buildQueryParam(message);
        // query
        String queryResultStr = psbcB2bQueryHelper.query(queryParam);
        // parse query result of string to message entity
        PsbcB2bMessage queryResult = psbcB2bQueryHelper.parseMesage(queryResultStr);
        if (PsbcConstants.PAY_RSP_CODE_SUCCESS.equals(queryResult.getRespCode())) {
            // verify
            boolean isVerify = (Boolean) psbcB2bCertHelper.verify(queryResult);
            if (isVerify) {
                psbcB2bQueryHelper.parsePlainMessage(queryResult);
            } else {
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
            }
        }
        return queryResult;
    }

    private PsbcB2bCertHelper psbcB2bCertHelper;
    private PsbcB2bQueryHelper psbcB2bQueryHelper;

    /**
     * @param psbcB2bCertHelper
     *            the psbcB2bCertHelper to set
     */
    public void setPsbcB2bCertHelper(PsbcB2bCertHelper psbcB2bCertHelper) {
        this.psbcB2bCertHelper = psbcB2bCertHelper;
    }

    /**
     * @param psbcB2bQueryHelper
     *            the psbcB2bQueryHelper to set
     */
    public void setPsbcB2bQueryHelper(PsbcB2bQueryHelper psbcB2bQueryHelper) {
        this.psbcB2bQueryHelper = psbcB2bQueryHelper;
    }

	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
